<?php
/**
 * Template Name: Kelola Cerita
 *
 * @GramediaWritingProject
 * @version 1.0
 * @Epifana & Vincentia
 */
 
 $bookid= isset( $_GET['bookid'] ) ? intval( $_GET['bookid'] ) : 0;
	$postbook = get_post($bookid);
	$title = $postbook->post_title;
	$sinopsis=$postbook->post_content;
	$thumburl= $_GET['thumburl'] ;
	$status = $postbook->post_status;
	$posttags = get_the_tags($bookid);
    foreach((array)$posttags as $tag) {
        $show_tags .= $tag->name . ',';
    }{
$rel_art .= rtrim($show_tags , ',');{
}
//print $rel_art;
}
	//$posttags = get_the_tags($bookid);
	//$tags ="";
	//if ($posttags) {
  //foreach($posttags as $tag) {
   //$tags .= $tag->name . " ,";
  //};
//}
 if( 'POST' == $_SERVER['REQUEST_METHOD']) 
 {
 if (!empty( $_POST['simpanpost'])){
 	$error = "";
	if (!empty($_POST['title'])) {
    $title = $_POST['title'];
   	 } else {
   	 	$error .= "Judul cerita ";
   	}
	if (!empty($_POST['description0'])&& trim($_POST['description0'])!=='') {
    		$sinopsis = $_POST['description0'];
   	 } else {
   		$error .= "Isi Sinopsis ";
   	}
	if(!empty($_POST['idbabnewsorted'])){
		$sortbab = $_POST['idbabnewsorted'];
		$sortbabnew = explode ("|", $sortbab);

		$i=1;
		foreach ($sortbabnew as $IDBAB)
		{
			update_post_meta((int)$IDBAB, 'number', $i);	
			$i = $i+1;
		}
	}
	
	
	if (empty($error)) {
			$tags = $_POST['post_tags'];
			if ($status=='publish'){
			$simpan_post = array(
			'post_title'	=>	$title,
			'post_content'	=>	$sinopsis,
			'ID' => $bookid,
			//'post_category'	=>	array($_POST['cat']),  // Usable for custom taxonomies too
			//'tags_input'	=>	array($tags)
			);
			}
			else {
			$simpan_post = array(
			'post_title'	=>	$title,
			'post_content'	=>	$sinopsis,
			'ID' => $bookid,
			'post_category'	=>	array($_POST['cat']),  // Usable for custom taxonomies too
			'tags_input'	=>	array($tags)
			);
	}
		wp_update_post($simpan_post);
		wp_redirect(get_site_url(). "/kelolacerita?bookid=".$bookid);
	}
} //end simpan post

else {
$error = "";
	if (!empty($_POST['title'])) {
    $title = $_POST['title'];
   	 } else {
   	 	$error .= "Judul cerita ";
   	}
	if (!empty($_POST['description0'])&& trim($_POST['description0'])!=='') {
    		$sinopsis = $_POST['description0'];
   	 } else {
   		$error .= "Isi Sinopsis ";
   	}
	if(!empty($_POST['idbabnewsorted'])){
		$sortbab = $_POST['idbabnewsorted'];
		$sortbabnew = explode ("|", $sortbab);

		$i=1;
		foreach ($sortbabnew as $IDBAB)
		{
			update_post_meta((int)$IDBAB, 'number', $i);
			$publish_bab = array(
			'post_status'	=>	'publish',
			'ID' => (int)$IDBAB,
			);
			wp_update_post($publish_bab);
			$i = $i+1;
		}
	}
	
	if (empty($error)) {
			$simpan_post = array(
			'post_title'	=>	$title,
			'post_content'	=>	$sinopsis,
			'post_category'	=>	array($_POST['cat']),  // Usable for custom taxonomies too
			'tags_input'	=>	array($_POST['post_tags']),
			'ID' => $bookid,
			'post_status' => 'publish',
			);
	
		wp_update_post($simpan_post);
	}
	wp_redirect(get_site_url().'/daftarcerita');
}
}
do_action('wp_insert_post', 'wp_insert_post');
 ?>
 
 <?php
 get_header();
 
?>
    <div class="col-md-12 column" >
	<div class="row clearfix">
		<input type="hidden" value=<?php echo $status;?> id="statusbuku" />
		<?php if (is_user_logged_in()) { get_template_part( 'kolompertama2' ); } else {wp_redirect(get_site_url());} ?>
	<div class='col-md-9 column' >
			 <!-- untuk kolom kedua yang dibagi 2 -->
		<div class="row clearfix">
		
				<div class="col-md-12 column">
				 <?php if(is_user_logged_in()){?>
				  
					<?php 
					        if (!empty($error)) {
	                            echo  "<strong>Tidak dapat memproses permintaan Anda, mohon isi dahulu : " . $error . ""  ;
	                        } else{
	                           // echo "Sudah disimpan"  ;
	                        }
	                    ?>
						
<form id="kelola_cerita" name="kelola_cerita" method="post" action="">

	<div class="judulceritaterbaru2" >Kelola Cerita</div>
    <div class="hr4"></div>
  	<div class="row">
                        <div class="col-md-2"><h5><strong>Judul  <?php if ($status=='draft') { echo " (Draft)"; }?>	</strong></h5></div>
                            <div class="col-md-9 ">
                                <div class="col-sm-12">
                                    <input class="form-control tinggitextbox" id="title0" type="text" name="title" placeholder="Judul Cerita Wajib diisi" 
									value="<?php echo $title;?>">
                                </div>
                            </div>
							<label id="errorjudulbuku" style="color:red; font-size: 10px;"></label>
   </div>
	<br/>
	<h5><strong>Bagian-bagian dalam cerita ini :</strong></h5>
	<div>
<!--	<table cellspacing="10" id="tablebab"> -->
<?php
							$mykey_values = get_post_custom_values( 'Chapter', $bookid );
								if (count($mykey_values) >0){
								$sort = array();
								$idbab = array();
                                foreach ( $mykey_values as $key => $babid ) 
										{
											
											$number = get_post_meta($babid, 'number', true);//get_post_custom_values ( 'number', $babid);
											array_push($sort, (int)$number);
											array_push($idbab, $babid);
										//sorting chapter berdasarkan urutan (number)
											array_multisort($sort,$idbab);
												
										}
											//var_dump($sort);
											//var_dump($idbab);
								//return array yang sudah di sorting

	?>							<table id="tablebab" class="table table-condensed" cellspacing="10" style="font-size:14px; color:#333333;"> 

								
					<?php			foreach ( $idbab as $sortedid => $babidsorted )
								 {
								    $chapter = get_post($babidsorted);
									$title = $chapter->post_title;
									$content = $chapter -> post_content;
									$status = $chapter->post_status;
									$link = get_permalink($bookid)."?pid=".$babidsorted . "&" .$title;
									$link2 = get_site_url()."/book/babbaru/?bookid=".$bookid."&pid=".$babidsorted . "&" .$title ."&edit=yes";
										if ($status=='draft'){
										
										echo "		<tr><td class='bb' style='width:85%;'><a href='$babidsorted' id='linkbab'>$title</a> (Draft) </td> <td style='width:15%;'><a  class='up'><span class='glyphicon glyphicon-arrow-up'></span>&nbsp;Up</a>  |  <a class='down'><span class='glyphicon glyphicon-arrow-down'></span>&nbsp;Down</a></td></tr>";

												
														}
										else {
									
										echo"		<tr><td class='bb' style='width:85%;'><a href='$babidsorted' id='linkbab'>$title </a></td><td style='width:15%;'><a  class='up'><span class='glyphicon glyphicon-arrow-up'></span>&nbsp;Up</a>  |  <a  class='down'><span class='glyphicon glyphicon-arrow-down'></span>&nbsp;Down</a></td></tr>";

													}
								 } 
								 		

?>
								</table>

<!--</table> -->
<?php 
} else { ?> Belum ada bab dalam cerita ini  <?php } 
?>
<div class="row">
   <br/>
   <?php if ($status=='draft') {?>	
   <div class="list-group-item" style="border-left:none; border-right:none;">
                        <div class="row">
                        <div class="col-md-2">Kategori</div>
                            <div class="col-md-9 ">
                                <div class="dropdown padd5">
								<?php 
								$args = array(
										'type'                     => 'post',
										'child_of'                 => 0,
										'parent'                   => '',
										'orderby'                  => 'name',
										'order'                    => 'ASC',
										'hide_empty'               => 0,
										'hierarchical'             => 0,
										'exclude'                  => '2,4,1',
										'include'                  => '',
										'number'                   => '',
										'taxonomy'                 => 'category',
										'pad_counts'               => false );

								$categories = get_categories( $args );
								$kategori = get_the_category($bookid); 
								$thecat = $kategori[0]->term_id;
								?>
								<select name="cat" id="cat" class="dropdowncat">
								<option value="-1">Silakan Pilih Kategori </option>
							<?php
								 foreach ($categories as $category) {
								 if ($category->term_id == $thecat ){
								 echo "<option value ='" . $thecat. "' selected='selected' style='color:red'>".$category->cat_name." (pilihan Anda)</option>";
								 }
								 else {
								$option = '<option value="'.$category->term_id .'">';
								$option .= $category->cat_name;
								//$option .= ' ('.$category->category_count.')';
								$option .= '</option>';
								echo $option; }} ?>
  </select>
								<?php //wp_dropdown_categories( 'show_option_none=Silakan Pilih Kategori&tab_index=10&hide_empty=0&class="dropdowncat"&exclude=2,4,1' ); ?>
                                  </div>
                            </div>
							<label id="errorkatbuku" style="color:red; font-size: 10px;"></label>
                        </div>
                    </div>
					<?php }?>
	<br/>
   <div class="col-md-2"><h5><strong>Sinopsis</strong></h5></div>
                <div class="col-md-12  panel-default">
				    <textarea class="kotaktext" name="description0" id="description0" ><?php 
					echo $sinopsis; ?>
                        </textarea>
                        </div>
				<label id="errorsinopsisbuku" style="color:red; font-size: 10px;"></label>
	</div>
	<br/>
	 <?php if ($status=='draft') {?>	
	 <div class="row">
                        <div class="col-xs-12">
                            <div class="input-group input-group-sm padd6">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-tags"></span></span>
                                <input type="text" class="form-control" name="post_tags" id="post_tags" placeholder="Tag wajib diisi, pisahkan tag dengan tanda ',' atau ';'" value="<?php echo  $rel_art; ?>" />
                            </div>
                        </div>
						<label id="errortagbuku" style="color:red; font-size: 10px;"></label>
				
      </div><?php }?>
				<br/>
				<a OnClick="DoSave();" class="btn btn-sm" > Simpan</a>
			<?php if ($status=='draft') {?>	
					<a OnClick="DoSaveandPublish();" class="btn btn-sm" > Simpan dan Tayangkan</a>
					<input type="submit" id="tayangkanpostok"  value="tayangkanpostok" style="display:none;" name="tayangkanpost" />
					
			<?php } ?>
				<input type="submit" id="simpanpostok"  value="simpanpost" style="display:none;" name="simpanpost" />
				<input type="hidden" name="idbabnewsorted" value="" id="idbabnew" ></input>
				<?php wp_nonce_field( 'new-post' ); ?>
</div>
</form>

<?php }
else
					{?>
                    
                  
                  
					<br/>
				<strong>	Bergabunglah dengan komunitas menulis di bawah naungan Gramedia Pustaka Utama. <br/>
					Gratis! </strong>
                    <br/><br/>
					  <div class="col-md-4 column" style="min-height:250px;" ><!-- untuk kolom pertama -->
					<?php 	 ?> 
                    
                                <div class="panel-group" id="panel-661231">
                                    <div class="panel panel-default">
                                        <div class="panel-heading">
                                            <table style="width:100%">
                                                <tr>
                                                    <td style="width:45%; text-align:center; border-right:thin solid #CCCCCC">
                                                    <a class="panel-title" data-toggle="collapse" data-parent="#panel-661231" href="#login">Masuk</a></td>
                                                    <td style="width:50%; text-align:center; ">
                                                    <a class="panel-title" data-toggle="collapse" data-parent="#panel-661231" href="#register">Daftar</a></td>
                                                </tr>
                                            </table>
                                        </div>
                                            <div id="login" class="panel-collapse collapse in">
                                            <form action="<?php echo get_option('home'); ?>/wp-login.php" method="post">
                                            <div class="panel-body">
                                                <div class="panel-body font1 utklogin">
                                                    <div class="input-group input-group-lg" >
                                                      <span class="input-group-addon glyphicon glyphicon-user"></span>
                                                      <input type="text" width="100%" class="form-control" placeholder="Username" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" size="20">
                                                      </input>
                                                    </div>
                                                    <div class="input-group input-group-lg" style="padding-bottom:5px; padding-top:5px;">
                                                     <span class="input-group-addon glyphicon glyphicon-log-in"></span>
                                                    <input type="password" name="pwd" id="pwd" size="20" class="form-control" placeholder="Password">
                                                    </input>
                                                    </div>
                                                    <input type="submit" name="submit" value="Masuk" class="btn btn-sm" />
                                                          
                                               <label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Ingat saya</label>
                                              <br/> <input type="hidden" name="redirect_to" value="<?php echo get_site_url()?>" />
                                            <?php    
                                                if($_GET['login'] == 'failed')
                                                {
                                                    ?>
                                                    <br/>
                                                    <button id="btnforgot" type="button" onclick="popup('popUpDiv')" class="btn btn-xs">
                                        <span>Lupa Password</span>
                                    </button>
                                        <div id="login-error" style="background-color: #FFEBE8;border:1px solid #C00;padding:5px;">
                                            <p>Anda salah memasukkan Username/Password. Silakan coba lagi.</p>
                                        
                                        </div>
                                         <div id="blanket" style="display:none;"></div>
                                        <div id="popUpDiv" style="display:none; top:40px; background-color:black;" ></form>
                                        <form name="lostpasswordform" id="lostpasswordform" action="<?php echo esc_url( site_url( 'wp-login.php?action=lostpassword', 'login_post' ) ); ?>" method="post">
                        <p>
                            <label for="user_login" ><?php _e('Username or E-mail:') ?><br />
                            <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr($user_login); ?>" size="20" /></label>
                        </p>
                        <?php
                        /**
                         * Fires inside the lostpassword <form> tags, before the hidden fields.
                         *
                         * @since 2.1.0
                         */
                        do_action( 'lostpassword_form' ); ?>
                        <input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
                        <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" class="btn btn-xs" value="<?php esc_attr_e('Get New Password'); ?>" /></p>
                    </form><br/>
                                        </div>
                                        <?php }?></div>
                                            </div>
                                            </form>
                                            </div>
                                            <div id="register" class="panel-collapse collapse">
                                            <div class="panel-body">
                                            <?php do_action('oa_social_login'); ?>
                                                <div class="panel-body font1">
                                                    <div class="panel-body font1 utklogin">
                                                    <div class="input-group input-group-lg" >
                                                      <span class="input-group-addon glyphicon glyphicon-user"></span>
                                                      <input type="text" class="form-control" placeholder="Username">
                                                    </div>
                                                    <div class="input-group input-group-lg" style="padding-bottom:5px; padding-top:5px;">
                                                      <span class="input-group-addon glyphicon glyphicon-log-in"></span>
                                                      <input type="password" class="form-control" placeholder="Password">
                                                    </div>
                                                    <div class="input-group input-group-lg" style="padding-bottom:5px;">
                                                      <span class="input-group-addon glyphicon glyphicon-new-window"></span>
                                                      <input type="password" class="form-control" placeholder="Confirm Password">
                                                    </div>
                                                    <button type="submit" class="btn btn-sm">Daftar</button>
                                                    </div>
                                                    </div>
                                                </div>
                                            </div>
                                    </div>
                                    
                                </div>
                                
								</div>
                  
					<?php }					?>
                    </div><?php //}					?>
           
       </div>      
	   </div>   	
	   
       </div><!-- end row clearfix -->
</div> <!--end col-md-12 column content-->
	<!-- end untuk content -->
<?php get_footer();?>